package dormouse.vfs.ui;

import java.util.HashSet;
import java.util.Set;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JComboBox;

import org.apache.commons.vfs2.FileObject;
import org.apache.commons.vfs2.FileSystemException;
import org.apache.commons.vfs2.VFS;

/**
 * This class maintains lists of locations that have been visited.
 * 
 * @author mfortner
 * 
 */
public class LocationComboBox extends JComboBox {

    /**
     * 
     */
    private static final long serialVersionUID = 6930059822770542253L;
    Stack<FileObject> previousStack = new Stack<FileObject>();
    Stack<FileObject> nextStack = new Stack<FileObject>();
    Set<FileObject> previousSet = new HashSet<FileObject>();
    Set<FileObject> nextSet = new HashSet<FileObject>();

    /**
     * Constructor
     */
    public LocationComboBox() {
        super();
        this.setEditable(true);
    }

    /**
     * Constructor
     * 
     * @param file
     *            The file object.
     */
    public LocationComboBox(FileObject file) {
        super();
        this.setEditable(true);
        setFileLocation(file);
    }

    public FileObject getFileLocation() {
        DefaultComboBoxModel model = (DefaultComboBoxModel) this.getModel();
        return resolveFile(model.getSelectedItem());
       
       
    }
    
    private FileObject resolveFile(Object selectedFile){
         FileObject selectedFo = null;
        if (selectedFile instanceof FileObject) {
            selectedFo = (FileObject) selectedFile;
        } else if (selectedFile instanceof String) {
            try {
                selectedFo = VFS.getManager().resolveFile((String) selectedFile);
            } catch (FileSystemException ex) {
                Logger.getLogger(LocationComboBox.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return selectedFo;
    }

    public void setFileLocation(String fileUrl) {
        try {
            setFileLocation(VFS.getManager().resolveFile(fileUrl));
        } catch (FileSystemException e) {
            e.printStackTrace();
        }
    }

    /**
     * This method sets the file location of the combo box.
     * 
     * @param file
     *            The new file location
     */
    public void setFileLocation(FileObject file) {

        if (file != null) {

            // put the previous location onto the stack
            if (this.getItemCount() > 0) {
               
                FileObject currFile = resolveFile(this.getSelectedItem());

                previousStack.push(currFile);
            }
            this.insertItemAt(file, 0);
            this.setSelectedIndex(0);
            this.fireActionEvent();

        }
    }

    /**
     * This method sets the current location
     */
    public void previous() {
        FileObject file = previousStack.pop();
        setFileLocation(file);
        if (!nextSet.contains(file)) {
            nextStack.push(file);
        }
    }

    /**
     * This method sets the next location
     */
    public void next() {
        FileObject file = this.nextStack.pop();
        setFileLocation(file);
        if (!previousSet.contains(file)) {
            previousStack.push(file);
        }
    }
}
